package hibu.LeiJonGun.customViews;

import hibu.LeiJonGun.activities.ActNetwork;
import hibu.LeiJonGun.seniorservice.R;
import hibu.LeiJonGun.seniorservice.sizeType;
import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
 * Written by: Christian L. Gunvaldsen
 * 
 * This classed is being used by 
 * ActNetwork and represent a user request
 * 
 * Has a accept and a decline button
 */
public class CustomViewUserRequest extends CustomAbstractViewGroup {
	private int btnUserTextSize = 0;
	private int marginTopButtons = 0;
	private TextView lblName;
	private Button btnAccept,btnDecline;
	
// the constructor
	
	public CustomViewUserRequest(Context context) {// not used
		super(context);
	}
	
	public CustomViewUserRequest(Context context, int viewIdx, String text, String username, sizeType screenSize) throws ClassCastException { // this one is being used
		super(context);
		if (!context.getClass().equals(ActNetwork.class)) throw new ClassCastException("Wrong context: "+context.getClass()); // to make sure its the right context
		this.username=username;
		this.screenSize=screenSize;
		this.viewIdx=viewIdx;
		getScreenSize(); // check screen size and get sizes for the view + text
		setSizes();
		initView(text);
	}
	
	private void initView(String text) { // custom for this textview
		this.setBackgroundResource(R.drawable.lbl_find_user);
		lblName = new TextView(this.getContext());
		lblName.setText(text);
		lblName.setTextColor(this.getResources().getColor(R.color.lblText));
		lblName.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
		lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		
		btnAccept = new Button(this.getContext());
		btnAccept.setBackgroundResource(R.drawable.btn_selector);
		btnAccept.setText(this.getResources().getString(R.string.networkBtnAccept));
		btnAccept.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnUserTextSize);
		btnAccept.setTag("Accept");
		//Listener for accept button:
		btnAccept.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String username = CustomViewUserRequest.this.getUsername();
				((ActNetwork)CustomViewUserRequest.this.getContext()).userRequestClicked(username,true);
			}
		});
		
		btnDecline = new Button(this.getContext());
		btnDecline.setBackgroundResource(R.drawable.btn_selector);
		btnDecline.setText(this.getResources().getString(R.string.networkBtnDecline));
		btnDecline.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnUserTextSize);
		btnDecline.setTag("Decline");
		//Listener for decline button:
		btnDecline.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				((ActNetwork)CustomViewUserRequest.this.getContext()).userRequestClicked(username,false);
			}
			
		});
		
	    //set position, size and text size
  		RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,marginTopPx); // width and height of the view
  		rlp.setMargins(0, marginTopPx * viewIdx, 0, 0); // (marginTopPx * viewIdx) if marginTopPx is 100 and the viewIdx is 1, the view will have a marginTop of 100
  		this.setLayoutParams(rlp);
  		setPadding();
  		
  		//add children views
  		this.addView(lblName);
  		this.addView(btnAccept);
  		this.addView(btnDecline);
	}
	
	private void setSizes() { // set size by screen sizes
		switch (screenSize) {
		case Small:
			btnUserTextSize = this.getResources().getDimensionPixelSize(R.dimen.nettworkBtnTextSizeSmall);
			marginTopPx = this.getResources().getDimensionPixelSize(R.dimen.lblUserRequestSizeSmall);
			marginTopButtons = padding+25;
			break;
		case Medium:
			btnUserTextSize = this.getResources().getDimensionPixelSize(R.dimen.nettworkBtnTextSizeMedium);
			marginTopPx = this.getResources().getDimensionPixelSize(R.dimen.lblUserRequestSizeMedium);
			marginTopButtons = padding+50;
			break;
		case Large:
			btnUserTextSize = this.getResources().getDimensionPixelSize(R.dimen.nettworkBtnTextSizeLarge);
			marginTopPx = this.getResources().getDimensionPixelSize(R.dimen.lblUserRequestSizeLarge);
			marginTopButtons = padding+50;
			break;
		case XLarge:
			btnUserTextSize = this.getResources().getDimensionPixelSize(R.dimen.nettworkBtnTextSizeXLarge);
			marginTopPx = this.getResources().getDimensionPixelSize(R.dimen.lblUserRequestSizeXLarge);
			marginTopButtons = padding+65;
			break;
		}
	}
	
	@Override
	protected void onLayout(boolean bool, int l, int t, int r, int b) { // position child views
		
		for (int i =0; i<this.getChildCount();i++) {
			View Child = this.getChildAt(i);
			if (Child.getClass().equals(TextView.class)) {t =padding;l = padding;} // user name
			else if (Child.getClass().equals(Button.class) && Child.getTag().equals("Accept")) { // Accept
				t= marginTopButtons;
				l = padding;
				r = (int)(viewWidth/3);
				b=viewHeight - padding;
			} else if (Child.getClass().equals(Button.class) && Child.getTag().equals("Decline")) { // Decline
				t = marginTopButtons;
				l = viewWidth-((int)(viewWidth/3));
				r = viewWidth-padding;
				b=viewHeight - padding;
			} 
			Child.layout(l, t, r, b);	
		}
		
	} // End onLayout	
}
